package guis.member;

import guis.RegisterPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JTable;

import utils.Date;
import classes.Address;
import classes.Classroom;
import classes.Gender;
import classes.Member;

import communication.Facade;

public class EditMemberWindow extends MemberWindow
{
	private static final long	serialVersionUID	= 1032307379765488755L;
	private Member				member;
	private Address				address;

	public EditMemberWindow( JFrame source, RegisterPanel< Member > registerPanel, Facade facade )
	{
		super( "Editar Membro", "editar", source, registerPanel, facade );
		createActions();
		loadClassroom();
	}

	private void createActions()
	{
		cancelButton.addActionListener( new ActionListener()
		{

			@Override
			public void actionPerformed( ActionEvent arg0 )
			{
				setVisible( false );
			}

		} );

		actionButton.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed( ActionEvent arg0 )
			{
				/*String name = nameTextField.getText();
				int day = getObjectIntValue( dayComboBox.getSelectedItem() );
				int month = getObjectIntValue( monthComboBox.getSelectedItem() );
				int year = getObjectIntValue( yearComboBox.getSelectedItem() );
				
				Gender gender = null;
				if( maleRadioButton.isSelected() ) 
				{
					gender = Gender.male;
				}
				else if( femaleRadioButton.isSelected() )
				{
					gender = Gender.female;
				}
				
				String phone = phoneTextField.getText();
				String eMail = eMailTextField.getText();
				
				String street = streetTextField.getText();
				String number = numberTextField.getText();
				String cep = cepTextField.getText();
				
				boolean comungant = comungantCheckBox.isSelected();
				
				District district = ( District ) districtComboBox.getSelectedItem();
				int districtId = 0;
				if( district != null ) districtId = district.getId();
				
				Classroom classroom = ( Classroom ) classroomComboBox.getSelectedItem();
				int classroomId = 0;
				if ( classroom != null ) classroomId = classroom.getId();
				
				Address address = new Address( street, number, cep, districtId );

				try
				{
					int addressId = facade.findId( address );
					Member member = new Member( name, gender, new Date( day, month, year ), addressId, phone, eMail, comungant, classroomId );
					
					facade.addMember( member );
					memberPanel.loadTable( null );
					setVisible( false );
				}
				catch ( RepositoryException e )
				{

				}*/
			}
			
			private int getObjectIntValue( Object object )
			{
				int ret;
				
				if( object != null && object instanceof Integer )
				{
					ret = ( Integer ) object;
				}
				else
				{
					ret = 0;
				}
				
				return ret;
			}

		} );
	}
	
	private void loadClassroom()
	{
		JTable table = memberPanel.getTable();
		int selectedIndex = table.getSelectedRow();
		
		int id = Integer.parseInt( ( String ) table.getModel().getValueAt( selectedIndex, 0 ) );
		String name = ( String ) table.getModel().getValueAt( selectedIndex, 1 );
		Gender gender = ( Gender ) table.getModel().getValueAt( selectedIndex, 2 );
		Date birth	= ( Date ) table.getModel().getValueAt( selectedIndex, 3 );
		String phone = ( String ) table.getModel().getValueAt( selectedIndex, 4 );
		String eMail = ( String ) table.getModel().getValueAt( selectedIndex, 5 );
		boolean comungant = ( Boolean ) table.getModel().getValueAt( selectedIndex, 6 );
		address = ( Address ) table.getModel().getValueAt( selectedIndex, 7 );
		Classroom classroom = ( Classroom ) table.getModel().getValueAt( selectedIndex, 8 );
		
		int addressId = 0;
		int classroomId = 0;
		if ( address != null ) addressId = address.getId();
		if ( classroom != null ) classroomId = classroom.getId();
		
		this.member = new Member( name, gender, birth, addressId, phone, eMail, comungant, classroomId );
		member.setId( id );
		
		nameTextField.setText( name );
		if( gender == Gender.male )
		{
			maleRadioButton.setSelected( true );
		}
		else if( gender == Gender.female )
		{
			femaleRadioButton.setSelected( true );
		}
		dayComboBox.setSelectedItem( birth.getDay() );
		monthComboBox.setSelectedItem( birth.getMonth() );
		yearComboBox.setSelectedItem( birth.getYear() );
		phoneTextField.setText( phone );
		eMailTextField.setText( eMail );
		comungantCheckBox.setSelected( comungant );
		classroomComboBox.setSelectedItem( classroom );
		
		if( address != null )
		{
			System.out.println("ok");
			streetTextField.setText( address.getName() );
			numberTextField.setText( address.getNumber() );
			cepTextField.setText( address.getCep() );
			//districtComboBox.setText( address.getDisctrict() );
			//cityTextField.setText( address.getCity() );
		}
	}
}
